package com.banzhi.shop.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.banzhi.shop.R;
import com.banzhi.shop.entity.GoodsDetails.Guige;
import com.banzhi.shop.entity.InviteShopkeeperShareDetails.GoodsShareDetails;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/11/16.
 */

public class GuigeAdapter3 extends BaseAdapter<GoodsShareDetails.OptionstitBean> {
    private GuigeAdapter3.GettextCallBack callBack;
    private int[] nn;

    private int width;

    public GuigeAdapter3(Context context, List<GoodsShareDetails.OptionstitBean> data) {
        super(context, data);
        if (data != null) {
            nn = new int[data.size()];
        }

    }

    @Override
    public View getView(final int ppp, View convertView, ViewGroup parent) {
        nn[ppp] = -1;
        final GoodsShareDetails.OptionstitBean data = getData().get(ppp);
        GuigeAdapter3.viewHolder holder = null;
        if (convertView == null) {
            holder = new GuigeAdapter3.viewHolder();
            convertView = getInflater().inflate(R.layout.item_guige, null);
            holder.tvguige = (TextView) convertView.findViewById(R.id.tv_guige);
            holder.tagFlowLayout = (TagFlowLayout) convertView.findViewById(R.id.id_flowlayout);

            convertView.setTag(holder);
        }
        holder = (GuigeAdapter3.viewHolder) convertView.getTag();
        holder.tvguige.setText(data.getTitle());
        final List<String> newList = new ArrayList<>();
        for (int i = 0; i < data.getItems().size(); i++) {
            newList.add(data.getItems().get(i).getTitle());
        }









        final GuigeAdapter3.viewHolder finalHolder = holder;
        holder.tagFlowLayout.setAdapter(new TagAdapter<String>(newList) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.item_guige_data,
                        finalHolder.tagFlowLayout, false);
                tv.setText(s);
                return tv;
            }

            @Override
            public boolean setSelected(int position, String s) {
                return s.equals("Android");
            }
        });



        //  当规格选择只有一个的时候，默认选中为红色，多个的时候则不用默认选中
        if (holder.tagFlowLayout.getAdapter().getCount()==1) {
            holder.tagFlowLayout.getAdapter().setSelectedList( 0 );
            callBack.gettext(data.getItems().get(0).getId(), ppp);
        }



        holder.tagFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                view.setEnabled(true);
                view.setClickable(false);
                if (nn[ppp] != position) {
                    nn[ppp] = position;
                    callBack.gettext(data.getItems().get(position).getId(), ppp);
                } else {
                    callBack.gettext(0, ppp);
                    nn[ppp] = -1;
                }
                return true;
            }
        });

        return convertView;
    }

    class viewHolder {
        TextView tvguige;
        TagFlowLayout tagFlowLayout;
    }

    public interface GettextCallBack {
        void gettext(int str, int position);
    }

    public void setCallBack(GuigeAdapter3.GettextCallBack callBack) {
        this.callBack = callBack;
    }
}
